package com.group.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.group.configurations.AlipayConfig;
import com.group.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.entity.Order;
import utils.BaseController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Api(tags = "订单信息")
@Controller
@RequestMapping(value = "/order",produces = "application/json;charset=utf-8")
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;

    //新增订单
    @ApiOperation("新增订单")
    @RequestMapping(value = "AddOrder.do")
    @ResponseBody
    public String addOrder(@RequestBody Order order, HttpServletResponse resp){
        System.out.println("新增订单:"+order);
        return writerSuccessResult(orderService.addOrder(order), resp);

    }
    //更新订单状态
    @ApiOperation("修改订单状态")
    @RequestMapping(value = "updateOrder.do")
    @ResponseBody
    public String updateStatus(@RequestBody Order order, HttpServletResponse resp) {
        System.out.println("修改订单状态:"+order);
        return writerSuccessResult(orderService.updateOrderStatus(order),resp);
    }

    //删除订单
    @ApiOperation("删除订单")
    @RequestMapping(value = "deleteOrder.do")
    @ResponseBody
    public String deleteOrder(@RequestBody Order order, HttpServletResponse resp){
        System.out.println("删除订单:"+order);
        return writerSuccessResult(orderService.deleteOrder(order),resp);
    }

    //查询订单状态
    @ApiOperation("查询订单状态")
    @RequestMapping(value = "searchOrderStatus.do")
    @ResponseBody
    public String searchOrderByStatus(@RequestBody Order order, HttpServletResponse resp){
        System.out.println("查询订单状态:"+order);

        return writerSuccessResult(orderService.searchOrderByStatus(order),resp) ;
    }

    //查询用户订单
    @ApiOperation("查询用户订单")
    @RequestMapping(value = "searchOrderByUserId.do")
    @ResponseBody
    public String searchOrderByOrder(@RequestBody Order order, HttpServletResponse resp){
        System.out.println("查询用户订单:"+order);
        return writerSuccessResult(orderService.searchOrderByUserId(order),resp);
    }

    //查询用户订单2.0
    @ApiOperation("查询用户订单2.0")
    @RequestMapping(value = "searchOrderByUserId2.do")
    @ResponseBody
    public String searchOrderByOrder2(@RequestBody Order order, HttpServletResponse resp){
        System.out.println("查询用户订单2:"+order);
        return writerSuccessResult(orderService.searchOrderByUserId2(order),resp);
    }
    //通过订单号查询订单
    @ApiOperation("订单号查询订单")
    @RequestMapping(value = "searchOrderByOrderNumber.do")
    @ResponseBody
    public String  searchOrderByOrderNumber(@RequestBody Order order, HttpServletResponse resp){
        System.out.println("订单号查询订单:"+order);
        return writerSuccessResult(orderService.searchOrderByOrderNumber(order),resp);
    }
    //通过订单号查询所有订单
    @ApiOperation("订单号查询订单")
    @RequestMapping(value = "searchAllOrderByOrderNumber.do")
    @ResponseBody
    public String  searchAllOrderByOrderNumber(@RequestBody Order order, HttpServletResponse resp){
        System.out.println("订单号查询所有订单:"+order);
        return writerSuccessResult(orderService.searchAllOrderByOrderNumber(order),resp);
    }

    //获得分页订单信息
    @ApiOperation("查询用户分页订单")
    @RequestMapping(value = "/getSomeOrder.do", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getSomeOrder(Long userId, Integer page, HttpServletResponse resp){
        List<Order> someOrder = orderService.getSomeOrder(userId, page);
        someOrder.forEach(System.out::println);
        String orderList = writerSuccessResult(someOrder, resp);
        return orderList;
    }

    @ApiOperation("订单支付")
    @RequestMapping(value = "/pay.do")
    @ResponseBody
    public void pay(Long orderNumber,HttpServletResponse response){
        System.out.println("pay--start");

        Order order = orderService.selectOrderByOrderId(orderNumber);
        String html = aliPay(order);
        //设置返回为网页
        response.setHeader("Content-Type","text/html;charset=utf-8");
        try {
            response.getWriter().write(html);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String aliPay(Order order) {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
//        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = order.getOrderNumber().toString();
        //付款金额，必填
        String total_amount = order.getMoney().toString();
        //订单名称，必填
        String subject = "订单名称";
        //商品描述，可空
        String body = "商品描述";

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }
}
